package com.qms.repast.ui.base;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.HashMap;

import butterknife.ButterKnife;

/**
 * @author 泅渡者
 * @date 2018/6/6 0006.
 * @describe:
 */

public abstract class BaseFragmentActivity extends RxAppCompatActivity {

	protected FragmentManager fragmentManager;
	protected FragmentTransaction fragmentTransaction;
	protected HashMap<String, Fragment> fragments;

	/**
	 * 是否沉浸式
	 */
	protected boolean isTrans;


	@Override
	protected void onCreate (@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(initLayout());
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			initStatusBar(false);
		}
		if (null != getIntent()) {
			handleIntent(getIntent());
		}
		ButterKnife.bind(this);
		initView();
	}

	/**
	 * 加载布局
	 *
	 * @return
	 */
	protected abstract int initLayout ();

	/**
	 * 获取上级传来的Intent
	 *
	 * @param intent
	 */
	protected abstract void handleIntent (Intent intent);

	/**
	 * 初始化布局
	 */
	protected abstract void initView ();

	@Override
	protected void onResume () {
		super.onResume();
	}

	@Override
	protected void onDestroy () {
		super.onDestroy();
	}

	@Override
	public void onDetachedFromWindow () {
		super.onDetachedFromWindow();
	}


	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	public void initStatusBar (boolean isTransparent) {
		Window window = getWindow();
		window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		if (isTransparent) {
			window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
																| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
		} else {
			window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
																| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
		}
		isTrans = isTransparent;
		window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
		window.setStatusBarColor(Color.WHITE);
	}

}
